package com.common.uilt;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;


public class ConfigUtil {
	public static String getFileIo(String key,String fileURL){
		Properties prop = new Properties();
		ServletContext rel=ServletActionContext.getServletContext();
		
		InputStream in;
		try {
			 in= new BufferedInputStream(new FileInputStream(rel.getRealPath(fileURL)));
			 prop.load(in);
			 return prop.getProperty(key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static String gerSerial(String path){
		StringBuffer sb= new StringBuffer();
		sb.append(ConfigUtil.getFileIo("serial.head", path));
		sb.append(ConfigUtil.getFileIo("serial.interval", path));
		sb.append(ConfigUtil.getFileIo("serial.no", path));
		return sb.toString();
	}
	public static void setprop(String key,String value,String path,String user){
		Properties prop = new Properties();
		ServletContext rel=ServletActionContext.getServletContext();
		OutputStream out=null;
		InputStream in;
		try {
			 in= new BufferedInputStream(new FileInputStream(rel.getRealPath(path)));
			 prop.load(in);
			 prop.put(key, value);
			 in.close();
			 out = new BufferedOutputStream(new FileOutputStream(rel.getRealPath(path)));  
			 prop.store(out, user+" updata '"+key+"' value "+DateUtil.SD2.format(new Date()));
			 out.flush();
			 out.close();
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		System.out.println(getFileIo("serial.head","serial.properties"));
	}
}
